home *** CD-ROM | disk | FTP | other *** search
/ MacHack 1997 / MacHack 1997.toast / Presentations / Presentations ’97 / Sessions ’97 / Multiplatform Code⁄Data Sharing / HelloBothWorlds / Libraries / resfile_flat.cpp < prev    next >
Encoding:
Text File  |  1997-06-26  |  1.5 KB  |  64 lines  |  [TEXT/CWIE]

  1.  
  2. // mail <chelly@eden.com> or surf http://www.eden.com/~chelly for feedback
  3. // free source code - do whatever you like with it
  4.  
  5. // universal flat resource file access
  6.  
  7. // file has table of contents with offsets to resource data, which comes later
  8.  
  9. #include "resfile_flat.h"
  10. #include "stream.h"
  11.  
  12. #include "resfile_flat_item.h"
  13.  
  14. resfile_flat::resfile_flat( stream* file ) : m_file( *file ) { }
  15.  
  16. resfile_flat::~resfile_flat() { }
  17.  
  18. // scan through table of contents to find resource, then read data
  19. void* resfile_flat::get_resource( long type, int id )
  20. {
  21.     // go to table of contents, at beginning of file
  22.     m_file.set_mark( 0 );
  23.     
  24.     long data_offset = m_file.get_mac_uint32();
  25.     
  26.     // get number of resources in file
  27.     // stream auto-swaps bytes if necessary
  28.     int resource_count = m_file.get_mac_uint16();
  29.     
  30.     // search through all resource entries
  31.     for ( int count = resource_count; count; --count )
  32.     {
  33.         // read current entry type and id
  34.         uint32 entry_type = m_file.get_mac_uint32();
  35.         uint16 entry_id = m_file.get_mac_uint16();
  36.  
  37.         // do they match?
  38.         if ( entry_type == type && entry_id == id )
  39.         {
  40.             // skip padding
  41.             m_file.skip( 2 );
  42.  
  43.             // get rest of info
  44.             long size = m_file.get_mac_uint32();
  45.             long offset = m_file.get_mac_uint32();
  46.             
  47.             // go to location in file of data
  48.             m_file.set_mark( offset + data_offset );
  49.             
  50.             // read and return resource data
  51.             char* data = new char [size];
  52.             if ( !data ) return nil;
  53.             m_file.get_bytes( data, size );
  54.             return data;
  55.         }
  56.  
  57.         // skip extra info
  58.         m_file.skip( 2 + 4 + 4 );
  59.     }
  60.     
  61.     return nil;
  62. }
  63.  
  64.